{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.2 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "0d008810b9c8467bcb3ca39aa2180e5b81b3a9acb136aab30d47954377cc5120"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import socket\n",
    "\n",
    "# def main():\n",
    "#     s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)\n",
    "#     txt = input('请输入你要发送的内容：')\n",
    "#     p = (\"172.18.104.161\", 8080)\n",
    "#     s.sendto(txt,p)\n",
    "#     s.close()\n",
    "    \n",
    "# if __name__ == '__main__':\n",
    "#     main()\n",
    "s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n",
    "txt = input('请输入你要发送的内容：')\n",
    "# txt= b'jdj'\n",
    "p = (\"172.18.104.161\", 8080)\n",
    "s.sendto(txt.encode('utf-8'),p)\n",
    "s.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "NameError",
     "evalue": "name 'socket' is not defined",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-854196653fa3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 1. 创建套接字\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mudp_socket\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msocket\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mAF_INET\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSOCK_DGRAM\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;31m# 2. 绑定一个本地信息\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mlocaladdr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m54167\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mudp_socket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlocaladdr\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 必须绑定自己电脑的ｉｐ以及ｐｏｒｔ，其他的不行\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'socket' is not defined"
     ]
    }
   ],
   "source": [
    "import socket\n",
    "# 1. 创建套接字\n",
    "udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n",
    "# 2. 绑定一个本地信息\n",
    "localaddr = (\"172.18.104.161\", 8080)\n",
    "udp_socket.bind(localaddr)  # 必须绑定自己电脑的ｉｐ以及ｐｏｒｔ，其他的不行\n",
    "\n",
    "# 3. 接收数据\n",
    "while True:\n",
    "    recv_data = udp_socket.recvfrom(1024)\n",
    "    # recv_data这个变量中存储的是一个元组(接收到的数据，(发送方的ip, port))\n",
    "    recv_msg = recv_data[0]  # 存储接收的数据\n",
    "    send_addr = recv_data[1]  # 存储发送方的地址信息\n",
    "    # 4. 打印接收到的数据\n",
    "    # print(recv_data)\n",
    "    # print(\"%s:%s\" % (str(send_addr), recv_msg.decode(\"utf-8\")))\n",
    "    print(\"%s:%s\" % (str(send_addr), recv_msg.decode(\"gbk\")))\n",
    "# 5. 关闭套接字\n",
    "udp_socket.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}